package com.lanou.moving.adapter;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lanou.moving.R;
import com.lanou.moving.entity.RankingListDetail;
import com.lanou.moving.entity.SingerSong;
import com.lanou.moving.entity.dao.Collection;
import com.lanou.moving.entity.dao.CollectionDao;
import com.lanou.moving.singleton.DaoSingleton;
import com.lanou.moving.utils.Exchange;
import com.lanou.moving.view.CommonProgressDialog;

import java.io.File;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by dllo on 15/10/26.
 */
public class SingerSongAdapter extends BaseAdapter {
    private Context context;
    private List<SingerSong.DataEntity> songs;
    private OnMenuClickListener onMenuClickListener;

    public SingerSongAdapter(Context context, List<SingerSong.DataEntity> songs) {
        this.context = context;
        this.songs = songs;
    }

    public void setOnMenuClickListener(OnMenuClickListener onMenuClickListener) {
        this.onMenuClickListener = onMenuClickListener;
    }

    public interface OnMenuClickListener {
        void onMenuClick(SingerSong.DataEntity songsEntity);
    }

    @Override
    public int getCount() {
        return songs != null && songs.size() > 0 ? songs.size() : 0;
    }

    @Override
    public SingerSong.DataEntity getItem(int position) {
        return songs.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.songs_list_listview_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final SingerSong.DataEntity songsEntity = getItem(position);

        if (songsEntity != null) {
            String favorites = Exchange.getCount(songsEntity.getFavorites());
            holder.songsListPosition.setText(position + 1 + "");
            holder.songsListSongName.setText(songsEntity.getName());
            holder.songsListItemSinger.setText(songsEntity.getSingerName());
            holder.songsListItemNumber.setText(favorites);
            holder.listMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onMenuClickListener != null) {
                        onMenuClickListener.onMenuClick(songs.get(position));
                    }
                }
            });
        }

        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'songs_list_listview_item.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.songs_list_position)
        TextView songsListPosition;
        @Bind(R.id.songs_list_songName)
        TextView songsListSongName;
        @Bind(R.id.songs_list_item_singer)
        TextView songsListItemSinger;
        @Bind(R.id.songs_list_item_number)
        TextView songsListItemNumber;
        @Bind(R.id.list_more)
        ImageView listMore;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
